package com.gitee.zhuyb.recursive;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0.0
 * @Description: 递归层级数据
 * @Date: 2021/12/25 8:51
 * @Copyright (C) ZhuYouBin
 */
public class RecursiveData {

    public static void main(String[] args) {
        RecursiveData recur = new RecursiveData();
        // 测试:获取测试数据
        List<OrgVo> data = recur.getData();
        // 获取层级数据
        List<OrgVo> orgVoList = recur.buildTree(data);
        System.out.println(orgVoList);
    }

    /**
     * 构建树形结构数据
     * @param orgVoList
     * @return
     */
    private List<OrgVo> buildTree(List<OrgVo> orgVoList) {
        // 获取所有最顶层机构
        List<OrgVo> topOrgList = new ArrayList<>();
        for (OrgVo orgVo : orgVoList) {
            // 父级机构编号等于-1则是最顶层机构数据
            if ("-1".equals(orgVo.getParentOrgNo())) {
                topOrgList.add(orgVo);
            }
        }
        // 获取顶层机构的下级机构
        List<OrgVo> result = new ArrayList<>();
        for (OrgVo orgVo : topOrgList) {
            OrgVo treeChildren = this.getTreeChildren(orgVo, orgVoList);
            result.add(treeChildren);
        }
        // 返回结果
        return result;
    }

    /**
     * 递归获取下级机构数据
     * @param parent 父级机构
     * @param orgVoList 所有机构数据集合
     * @return
     */
    private OrgVo getTreeChildren(OrgVo parent, List<OrgVo> orgVoList) {
        // 子级机构集合
        List<OrgVo> childrenList = new ArrayList<>();
        // 遍历所有机构数据,获取当前parent机构的直接下级机构数据
        for (OrgVo orgVo : orgVoList) {
            // 判断是否为parent的子级机构
            if (parent.getOrgNo().equals(orgVo.getParentOrgNo())) {
                // 递归调用,获取当前orgVo机构的子级机构
                childrenList.add(this.getTreeChildren(orgVo, orgVoList));
            }
        }
        // 循环结束,设置parent的子级机构
        parent.setChildren(childrenList);
        return parent;
    }

    /**
     * 准备数据集
     * @return
     */
    private List<OrgVo> getData() {
        String jsonData = "[{\"orgNo\":\"1001\",\"orgName\":\"福建省\",\"parentOrgNo\":\"-1\"}," +
                "{\"orgNo\":\"100101\",\"orgName\":\"厦门市\",\"parentOrgNo\":\"1001\"}," +
                "{\"orgNo\":\"10010101\",\"orgName\":\"湖里区\",\"parentOrgNo\":\"100101\"}," +
                "{\"orgNo\":\"10010102\",\"orgName\":\"思明区\",\"parentOrgNo\":\"100101\"}," +
                "{\"orgNo\":\"100102\",\"orgName\":\"福州市\",\"parentOrgNo\":\"1001\"}," +
                "{\"orgNo\":\"100103\",\"orgName\":\"泉州市\",\"parentOrgNo\":\"1001\"}," +
                "{\"orgNo\":\"1002\",\"orgName\":\"江西省\",\"parentOrgNo\":\"-1\"}," +
                "{\"orgNo\":\"100201\",\"orgName\":\"上饶市\",\"parentOrgNo\":\"1002\"}," +
                "{\"orgNo\":\"10020101\",\"orgName\":\"上饶县\",\"parentOrgNo\":\"100201\"}," +
                "{\"orgNo\":\"10020102\",\"orgName\":\"余干县\",\"parentOrgNo\":\"100201\"}," +
                "{\"orgNo\":\"100202\",\"orgName\":\"南昌市\",\"parentOrgNo\":\"1002\"}]";
        // json数据转换为实体类
        List<OrgVo> orgVoList = JSON.parseArray(jsonData, OrgVo.class);
        return orgVoList;
    }

}
